#!/usr/bin/python3

#pip3 install xlrd==1.2.0
import xlrd
import time

print("Hello, World!")

#打开excel
wb = xlrd.open_workbook('language_entry_table.xlsx')

#按工作簿定位工作表
table = wb.sheet_by_name('index')

#print(table.nrows)#有效数据行数
#print(table.ncols)#有效数据列数
#print(table.cell(0,0).value)#输出第一行第一列的值
#print(table.row_values(0))#输出第一行的所有值
#将数据和标题组合成字典
#print(dict(zip(table.row_values(0),table.row_values(1))))

index_prefix = "LANGUAGE_INDEX_"

def upalgin(num):
    ret = (num/4)
    if (num%4) > 0 :
        ret = ret + 1
    return (4 * ret) + 4

def format_str_get(num):
    return "{: <"+str(int(upalgin(num)))+"}"

row_max_array = []
#计算每列的最大宽度
for col in range(0,table.ncols):
    col_array = []
    for row in range(1,table.nrows):
        col_array.append( len(str(table.cell(row,col).value)))
    row_max_array.append( max(col_array) )
    
print(row_max_array)




#输出.h头文件
inc_file = open("language_entry_table.h", "wb")

def inc_file_out(string):
    inc_file.write(bytes(string,'utf8'))

inc_file_out("#ifndef LANGUAGE_ENTRY_TABLE_H\n")
inc_file_out("#define LANGUAGE_ENTRY_TABLE_H\n\n")

inc_file_out("//datetime:"+time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())+"\n")
inc_file_out("#include <stdint.h>\n")
inc_file_out("#include <stddef.h>\n\n")

inc_file_out("#ifdef __cplusplus\n")
inc_file_out("extern \"C\" {\n")
inc_file_out("#endif\n")

#格式化宽度
formatstr = format_str_get( len(str(index_prefix)) + row_max_array[1] )
print("inc width: "+formatstr + "\n")
inc_file_out("//inc width: "+formatstr+ "\n")
inc_file_out(str("enum {\n"))
inc_file_out(str("    "+formatstr.format(index_prefix+"0")+" = 0,\n"))
for row in range(1,table.nrows):
    inc_file_out("    "+formatstr.format(index_prefix+str(table.cell(row,1).value))+" = "+str(row)+", //" + str(table.cell(row,2).value) + "\n" )
inc_file_out(str("    "+formatstr.format(index_prefix+"MAX")+" = "+str(table.nrows)+"\n"))
inc_file_out(str("};\n\n")) 

inc_file_out(str("#ifdef __cplusplus\n")) 
inc_file_out(str("} /*extern \"C\"*/\n")) 
inc_file_out(str("#endif\n\n")) 

inc_file_out(str("#endif\n")) 
inc_file.close()



#输出.c源文件
src_file = open("language_entry_table.c", "wb")

def src_file_out(string):
    src_file.write(bytes(string,'utf8'))
    
src_file_out("#include <stdint.h>\n")
src_file_out("#include <stddef.h>\n\n")
src_file_out("#include \"language_entry.h\"\n")
src_file_out("#include \"language_entry_table.h\"\n\n")


src_file_out(str("const language_entry_t g_entry_table[] = \n"))
src_file_out(str("{\n"))
src_file_out("    {")
formatstr = format_str_get( len(str(index_prefix)) + row_max_array[1] )
src_file_out(formatstr.format(index_prefix+"0,") )
for col in range(2,table.ncols):
    formatstr = format_str_get( row_max_array[col] )
    src_file_out(formatstr.format("\"\",") )
src_file_out("},\n")

for row in range(1,table.nrows):
    src_file_out("    {")
    formatstr = format_str_get( len(str(index_prefix)) + row_max_array[1] )
    src_file_out(formatstr.format(index_prefix+str(table.cell(row,1).value)+",") )
    for col in range(2,table.ncols):
        formatstr = format_str_get( row_max_array[col] )
        src_file_out(formatstr.format("\""+str(table.cell(row,col).value)+"\",") )
    src_file_out("},\n")
src_file_out(str("};\n"))   
src_file.close()

uni  = index_prefix.encode('unicode-escape').decode()
print( uni )